<template>
  <div class="">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <ProFilter v-model="filter.id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <CustomerSelect v-model="filter.ctm_id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" prop="begin_order_time">
          <el-date-picker
            v-model="filter.begin_order_time"
            placeholder="开始时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" prop="end_order_time">
          <el-date-picker
            v-model="filter.end_order_time"
            placeholder="结束时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList">查 询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border 
      :summary-method="getSummaries"
      show-summary
      fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="客户名称" width="260px" prop="lc_name"/>
      <el-table-column label="项目名称" width="260px" prop="name"  :formatter="paytypeFormatter" />
      <el-table-column label="PO单号" width="190px">
        <template slot-scope="{row}">
          {{ 'PO'+row.id }}
        </template>
      </el-table-column>
      <el-table-column label="下单日期" width="110px" align="center" prop="order_time" />
      <el-table-column label="付款方式" width="110px">
        <template slot-scope="{row}">
          <span v-for="(item, index) in row.payments" :key="index" class="mr10">
            <a style="color: blue" 
            @click="handleDialog({...{pro_id: row.id}, ...
                {dRef: 'paytypes', dTitle: '项目付款方式', dType: 'edit', dField: dFieldPaytypes}})"
            >{{ item.v + ": " + item.s + "%" }}</a><br>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.trading_price, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="扣款金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.deduction, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="追加金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(row.money_bx, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="回款金额" align="right" width="140px">
        <template slot-scope="{row}">
          <a
            href="javascript:void(0)"
            style="color: blue" 
            @click="handleDialog({...{ 'pro_id': row.id }, ...
            {dRef: 'xshk', dTitle: '项目回款信息', dType: 'edit', dField: dFieldXSHK}})"
          >{{ app.toFix(row.xshk_money, 2) }}</a>
        </template>
      </el-table-column>
      <el-table-column label="未回款" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(app.toFix(row.trading_price, 2) - app.toFix(row.xshk_money, 2) - row.deduction + row.money_bx, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="已开票金额" align="right" width="140px">
        <template slot-scope="{row}">
          <a
            href="javascript:void(0)"
            style="color: blue" 
            @click="handleDialog({...{ 'pro_id': row.id }, ...
            {dRef: 'proinvoice', dTitle: '开票信息', dType: 'edit', dField: dFieldProInvoice}})">
          {{ app.toFix(row.has_tax, 2) }}</a>
        </template>
      </el-table-column>
      <el-table-column label="未开票金额" align="right" width="140px">
        <template slot-scope="{row}">
          {{ app.toFix(app.toFix(row.trading_price, 2) - app.toFix(row.has_tax, 2)  - row.deduction + row.money_bx, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="结算" width="80px" align="center" :formatter="incomeState" />
      <el-table-column label="备注" min-width="260px" prop="order_remark" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                :disabled="row.income_state && row.income_state === 1 ? true : false"
                @click.native="handleDialog({...{'id': row.id},
                                             ...{dTitle: '财务结算', dType: 'edit', dField: dField}})"
              >
                结 算
              </el-dropdown-item>
              <el-dropdown-item
                @click.native="files({e_id: row.id, type: '',
                                      db_type: 'logc_pro_archives'})"
              >
                附 件
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    <Cos ref="cos" :info="dialog" />
    <Paytypes ref="paytypes" :get-data="getList" :info="dialog" />
    <XSHK ref="xshk" :get-data="getList" :info="dialog" />
    <ProInvoice ref="proinvoice" :info="dialog" />
  </div>
</template>

<script>
import ProFilter from '@/components/ProFilter'
import { incomeStatistics } from '@/api/finance/income'
import { pros } from '@/api/project'
import Dialog from './components/income-dialog'
import ProInvoice from './components/pro-invoice-dialog'
import Paytypes from '@/views/business/components/order/components/paytypes-dialog'
import XSHK from '@/views/business/components/order/components/xshks-dialog'
import page from '@/utils/page'
import CustomerSelect from '@/myComponents/CustomerSelect'

export default {
  name: '',
  components: { ProFilter, Dialog, Paytypes, XSHK, ProInvoice, CustomerSelect },
  extends: page,
  props: {
    reqList: {
      default: pros,
      type: Function
    }
  },
  data() {
    return {
      parameter: { states: '1,3' },
      dField: ['id', 'state', 'order_remark'],
      dFieldPaytypes: ['pro_id', 'trading_price'],
      dFieldXSHK: ['pro_id', 'paytype', 'trading_price'],
      dFieldProInvoice: ['pro_id'],
      price: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    incomeState(e) {
      let s = '未结算'
      if (e.state && e.state === 3) s = '已结算'
      return s
    },
    paytypeFormatter(e, c) {
      const xshk = e.xshk
      const xshks = []
      if (xshk) {
        const array = xshk.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('#')
            const f = {}
            f.v = js[0]
            f.m = js[1]
            f.t = js[2]
            xshks.push(f)
          })
        }
      }
      e.xshks = xshks

      const fund = e.fund
      const funds = []
      if (fund) {
        const array = fund.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('#')
            const f = {}
            f.v = js[0]
            f.m = js[1]
            f.t = js[2]
            funds.push(f)
          })
        }
      }
      e.funds = funds

      const payment = e.payment
      const payments = []
      if (payment) {
        const array = payment.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('-')
            const f = {}
            f.v = js[0] || ''
            f.s = js[3] || ''
            payments.push(f)
          })
        }
      }
      e.payments = payments

      return e.name
    },
    getListAfter(e) {
      this.price = e.summary
    },
    getSummaries() {
      const sums = []
      sums[0] = '合计'
      sums[6] = this.app.toFix(this.price.trading_price, 2)
      sums[7] = this.app.toFix(this.price.deduction, 2)
      sums[8] = this.app.toFix(this.price.money_bx, 2)
      sums[9] = this.app.toFix(this.price.xshk_money, 2)
      sums[10] = this.app.toFix((this.price.trading_price - this.price.xshk_money + this.price.money_bx - this.price.deduction), 2)
      sums[11] = this.app.toFix(this.price.has_tax, 2)
      sums[12] = this.app.toFix(this.price.trading_price - this.price.has_tax + this.price.money_bx - this.price.deduction, 2)
      return sums
    }
  }
}
</script>
